package com.fitness.front.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.front.convertor.MemberConvertor;
import com.fitness.front.exception.BizException;
import com.fitness.front.mapper.MemberMapper;
import com.fitness.front.modal.member.dto.MemberDto;
import com.fitness.front.modal.member.dto.RegisterFormDto;
import com.fitness.front.modal.member.entity.Member;
import com.fitness.front.modal.member.param.MemberPageQueryParams;
import com.fitness.front.modal.member.param.MemberQueryParams;
import com.fitness.front.modal.member.vo.MemberVO;
import com.fitness.front.service.IMemberService;
import com.fitness.front.utils.EncryptUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

@Service
@RequiredArgsConstructor
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    private final MemberConvertor memberConvertor;

    @Override
    public boolean register(RegisterFormDto registerFormDto) {
        String username = registerFormDto.getUsername();
        String mobile = registerFormDto.getMobile();

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).or().eq("mobile", mobile);
        if(this.baseMapper.selectCount(queryWrapper) > 0){
            throw new BizException("用户名或手机号已存在");
        }

        Member member = new Member();
        member.setUsername(registerFormDto.getUsername());
        member.setRealName(registerFormDto.getRealName());
        member.setMobile(registerFormDto.getMobile());
        member.setAge(registerFormDto.getAge());
        member.setHeight(registerFormDto.getHeight());
        member.setWeight(registerFormDto.getWeight());
        member.setGender(registerFormDto.getGender());
        member.setSalt(EncryptUtil.genRandomSalt());
        member.setPassword(EncryptUtil.encryptPassword(registerFormDto.getPassword(),member.getSalt()));

        return this.baseMapper.insert(member) > 0;
    }

    @Override
    public Member findByMobile(String uuid, String mobile) {
        return null;
    }

    @Override
    public Boolean resetByMobile(String uuid, String password) {
        return null;
    }

    @Override
    public Member editOwn(MemberDto memberEditDTO) {
        return null;
    }

    @Override
    public void logout() {
        StpUtil.logout();
    }

    @Override
    public Boolean modifyPass(Long memberId,String salt, String newPassword) {
        String updatedPassword = EncryptUtil.encryptPassword(newPassword,salt);
        return lambdaUpdate().set(Member::getPassword,updatedPassword).eq(Member::getId,memberId).update();
    }

    @Override
    public IPage<MemberVO> getMemberPage(MemberQueryParams queryParams) {
        return null;
    }

    @Override
    public Boolean forbidden(Long memberId, Boolean diable) {
        return null;
    }

    @Override
    public Boolean resetPass(Long memberId, String newPassword) {
        return null;
    }

    @Override
    public Page<MemberVO> getUserPage(MemberPageQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Member> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<Member> userBoPage = this.baseMapper.getMemberPage(page, queryParams);

        return memberConvertor.entity2Vo(userBoPage);
    }

    @Override
    public boolean saveBatch(Collection<Member> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Member> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Member> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Member entity) {
        return false;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Member> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Member> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public Class<Member> getEntityClass() {
        return null;
    }
}
